//
//  ShenPiJiLuListViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/2/22.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "ShenPiJiLuListViewController.h"
#import "ShenPiJiLuListTableViewCell.h"
#import "ShenPiXiangQingViewController.h"
#import "KeHuXiangQingViewController.h"
@interface ShenPiJiLuListViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>{
    int page;
    NSMutableArray *dataSource;
}
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingTableView *mainTableView;

@end

@implementation ShenPiJiLuListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.customNavBar.hidden = YES;
    
    if (@available(iOS 11.0, *)) {
        self.mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"ShenPiJiLuListTableViewCell" bundle:nil] forCellReuseIdentifier:@"ShenPiJiLuListTableViewCell"];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self downRefresh];
    }];

    _mainTableView.mj_header = header;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self upRefresh];
    }];
    _mainTableView.mj_footer = footer;
    
    page = 1;
    [self getInfoFromServer];
    
}


//上拉刷新
- (void)upRefresh {
    page++;
    [self getInfoFromServer];
}

//下拉刷新
- (void) downRefresh {
    page = 1;
    [self getInfoFromServer];
}

- (void)getInfoFromServer{
    
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    [paraDic setValue:[NSString stringWithFormat:@"%d",page] forKey:@"page"];
    [paraDic setValue:@10 forKey:@"limit"];
    [paraDic setValue:@"" forKey:@"categoryId"];
    [paraDic setValue:_status forKey:@"status"];
    [paraDic setValue:@"" forKey:@"search"];
    
    NSString *url = @"/examineWaiting/queryOaExamineList";
    
    if([_status isEqualToString:@"3"]){
        [paraDic setValue:@"" forKey:@"status"];
        url = @"/oaExamine/myInitiate";
    }

    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:url andParas:paraDic andShowHud:YES andTransferGottenData:^(id info, NSError *error) {
        if (!error) {
            NSLog(@"info:%@",info);
            NSArray *array = [[info objectForKey:@"data"] objectForKey:@"list"];
            if (page == 1) {
                if ([array count]> 9) {
                    dataSource = [NSMutableArray arrayWithArray:array];
                    [_mainTableView.mj_header endRefreshing];
                    [_mainTableView.mj_footer endRefreshing];
                }else {
                    dataSource = [NSMutableArray arrayWithArray:array];
                    [_mainTableView.mj_header endRefreshing];
                    [_mainTableView.mj_footer endRefreshingWithNoMoreData];
                    if(dataSource.count == 0){
                        _mainTableView.mj_footer.hidden = YES;
                    }else{
                        _mainTableView.mj_footer.hidden = NO;
                    }
                }
            }else {
                if ([array count]> 9) {
                    [dataSource addObjectsFromArray:array];
                    [_mainTableView.mj_header endRefreshing];
                    [_mainTableView.mj_footer endRefreshing];
                }else {
                    NSArray *tempArray = array;
                    if (tempArray.count > 0) {
                        [dataSource addObjectsFromArray:tempArray];
                    }
                    [_mainTableView.mj_header endRefreshing];
                    [_mainTableView.mj_footer endRefreshingWithNoMoreData];
                }
            }
            
            [_mainTableView reloadData];
            
            
        }else {
            [_mainTableView.mj_header endRefreshing];
            [_mainTableView.mj_footer endRefreshing];
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dataDic = dataSource[indexPath.row];
    AllModel *model = [[AllModel alloc] init];
    model.modelInfoDictionary = dataDic;
    return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[ShenPiJiLuListTableViewCell class] contentViewWidth:[self cellContentViewWith]];
//    return 190;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShenPiJiLuListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShenPiJiLuListTableViewCell"];
    NSDictionary *dataDic = dataSource[indexPath.row];
    AllModel *model = [[AllModel alloc] init];
    model.modelInfoDictionary = dataDic;
    cell.model = model;
    cell.userInfoClickBlock = ^(NSString * _Nonnull customerId) {
        KeHuXiangQingViewController *xiangQing = [[KeHuXiangQingViewController alloc] init];
        xiangQing.customerId = customerId;
        [self pushViewController:xiangQing];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ShenPiXiangQingViewController *xiangQing = [[ShenPiXiangQingViewController alloc] init];
    NSDictionary *dataDic = dataSource[indexPath.row];
    xiangQing.recordId = GetHandleString(dataDic, @"examineRecordId");
    xiangQing.examineId = GetHandleString(dataDic, @"examineId");
    if(_status.integerValue == 1){
        xiangQing.isDaiBan = YES;
    }else{
        xiangQing.isDaiBan = NO;
    }
    xiangQing.manageClickResultBlock = ^{
        [self downRefresh];
    };
    [self pushViewController:xiangQing];
}


-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"暂无搜索"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"暂无数据~"attributes: @{NSFontAttributeName: [UIFont systemFontOfSize:16],NSForegroundColorAttributeName: rgba(163, 168, 185, 1)}];
    return string;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


